package com.hzeng.util;

import java.io.File;
import java.util.List;
import java.util.Map;

import au.com.bytecode.opencsv.CSVWriter;

public class ProgramRecorder{
	private static ProgramRecorder recorder = new ProgramRecorder();
	
	private ProgramRecorder() {
		
	}
	
	public static ProgramRecorder getInstance() {
		if (recorder == null) {
			recorder = new ProgramRecorder();
		}
		return recorder;
	}
	
	private boolean recordFlag = false;
	private File saveDirectory = null;
	private List<Long> pids = null;
	private Map<Long,CSVWriter> writers = null;
	
	public File getSaveDirectory() {
		return saveDirectory;
	}

	public void setSaveDirectory(File saveDirectory) {
		this.saveDirectory = saveDirectory;
	}

	public List<Long> getPids() {
		return pids;
	}

	public void setPids(List<Long> pids) {
		this.pids = pids;
	}

	public boolean isRecordFlag() {
		return recordFlag;
	}

	public void setRecordFlag(boolean recordFlag) {
		this.recordFlag = recordFlag;
	}
	
	
	public void recordDataInFile(List<Program> programs) {
		if (saveDirectory == null) {
			
		}else {
			if (saveDirectory.isDirectory()) {
				if (programs == null) {
					
				}else if (programs.size() > 0) {
					//add code here,fix open and close writer every time.
					if (recordFlag) {
						if (writers == null || writers.size() < 1) {
							writers = CSVUtil.createCSVFiles(saveDirectory, programs);
						}
						
						CSVUtil.appendData(writers,programs);
					}else {
						CSVUtil.closeWriters(writers);
					}
					
				}
			}
		}
	}
}
